#include "rolemanagerapi.h"

RoleManagerApi::RoleManagerApi(QObject *parent)
    : QObject{parent}
{
    baseUrl = Config::baseUrl;
    manager = new QNetworkAccessManager(0);
    request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));//post请求设置为json，才能发送josn格式文件
    request.setRawHeader("Authorization",  qApp->property("token").value<QString>().toLocal8Bit());
}

QByteArray *RoleManagerApi::list(int pageNum, int pageSize, QString roleName, QString roleKey, QString status, QString beginTime, QString endTime)
{
    QString params = "system/role/list?pageNum="+ QString::number(pageNum) +
                     "&pageSize=" + QString::number(pageSize);
    if(!roleName.isEmpty()){
        params += "&roleName=" + roleName;
    }
    if(!roleKey.isEmpty()){
        params += "&roleKey=" + roleKey;
    }
    if(!status.isEmpty()){
        params += "&status=" + status;
    }
    if(beginTime != "2000/1/1" && endTime != "2000/1/1"){
        params += "&params%5BbeginTime%5D=" + beginTime + "&params%5BendTime%5D=" + endTime;
    }
    request.setUrl(QUrl(baseUrl + params));
    QNetworkReply* reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::sys_normal_disable()
{
    request.setUrl(baseUrl + "system/dict/data/type/sys_normal_disable");
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::treeSelect()
{
    request.setUrl(baseUrl + "system/menu/treeselect");
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::addRole(Role *role){
    request.setUrl(baseUrl + "system/role");
    QJsonObject obj;
    obj.insert("deptCheckStrictly",role->deptCheckStrictly);
    obj.insert("menuCheckStrictly",role->menuCheckStrictly);
    QJsonArray arr;
    for(int i = 0;i< role->menuIds.size();i++){
        arr.append(role->menuIds.at(i));
    }
    obj.insert("menuIds",arr);
    obj.insert("remark",role->remark);
    obj.insert("roleName",role->roleName);
    obj.insert("roleKey",role->roleKey);
    obj.insert("roleSort",role->roleSort);
    obj.insert("status",role->status);
    QJsonDocument doc = QJsonDocument(obj);
    QByteArray bytes = doc.toJson();
    QNetworkReply *reply= manager->post(request,bytes);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::updateRole(Role *role)
{
    request.setUrl(baseUrl + "system/role");
    QJsonObject obj = role->toJsonObject();
    QJsonDocument doc = QJsonDocument(obj);
    QByteArray bytes = doc.toJson();
    QNetworkReply *reply= manager->put(request,bytes);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::getById(QString id)
{
    request.setUrl(baseUrl + "system/role/" + id);
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::getMenuById(QString id)
{
    request.setUrl(baseUrl + "system/menu/roleMenuTreeselect/" + id);
    QNetworkReply *reply= manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::deleteItems(QString ids)
{
    request.setUrl(baseUrl + "system/role/" + ids);
    QNetworkReply * r = manager->deleteResource(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::changeStatus(QString id, QString status)
{
    request.setUrl(baseUrl + "system/role/changeStatus");
    QJsonObject obj;
    obj["roleId"] = id;
    obj["status"] = status;
    QJsonDocument doc = QJsonDocument(obj);
    QByteArray byte = doc.toJson();
    QNetworkReply * r = manager->put(request,byte);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::deptTree(QString id)
{
    request.setUrl(baseUrl + "system/role/deptTree/" + id);
    QNetworkReply * r = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::allocatedList(int pageNum, int pageSize, QString roleId,
                                          QString userName,QString phone)
{
    QString params = "system/role/authUser/allocatedList?pageNum="+ QString::number(pageNum) +
                     "&pageSize=" + QString::number(pageSize) +
                     "&roleId=" + roleId;
    if(!userName.isEmpty()){
        params += "&userName=" + userName;
    }
    if(!phone.isEmpty()){
        params += "&phonenumber=" + phone;
    }
    request.setUrl(QUrl(baseUrl + params));
    QNetworkReply* reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::unallocatedList(int pageNum, int pageSize, QString roleId, QString userName, QString phone)
{
    QString params = "system/role/authUser/unallocatedList?pageNum="+ QString::number(pageNum) +
                     "&pageSize=" + QString::number(pageSize) +
                     "&roleId=" + roleId;
    if(!userName.isEmpty()){
        params += "&userName=" + userName;
    }
    if(!phone.isEmpty()){
        params += "&phonenumber=" + phone;
    }
    request.setUrl(QUrl(baseUrl + params));
    QNetworkReply* reply = manager->get(request);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::cancelAuth(QString userId, QString roleId)
{
    QString params = "system/role/authUser/cancel";
    request.setUrl(QUrl(baseUrl + params));
    QJsonObject obj;
    obj.insert("userId",userId);
    obj.insert("roleId",roleId);
    QJsonDocument doc = QJsonDocument(obj);
    QByteArray byte = doc.toJson();
    QNetworkReply* reply = manager->put(request,byte);
    //同步请求
    QEventLoop eventLoop;
    connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(reply->readAll().data());
    reply->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::cancelAll(QString roleId,QString ids)
{
    request.setUrl(baseUrl + "system/role/authUser/cancelAll?roleId=" + roleId
                   + "&userIds=" + ids);
    QByteArray arr;
    QNetworkReply * r = manager->put(request,arr);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::selectAll(QString roleId, QString ids)
{
    request.setUrl(baseUrl + "system/role/authUser/selectAll?roleId=" + roleId
                   + "&userIds=" + ids);
    QByteArray arr;
    QNetworkReply * r = manager->put(request,arr);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}

QByteArray *RoleManagerApi::dataScope(Role role)
{
    request.setUrl(baseUrl + "system/role/dataScope");
    QJsonDocument doc = QJsonDocument( role.toJsonObject());
    QByteArray arr = doc.toJson();
    QNetworkReply * r = manager->put(request,arr);
    //同步请求
    QEventLoop eventLoop;
    connect(r, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
    eventLoop.exec();
    data = new QByteArray(r->readAll().data());
    r->deleteLater();
    utils.requestCheck(data);
    return data;
}


